//
//  CheckCodeViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/29.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "CheckCodeViewController.h"
#import "AFNetworking.h"
#import "UIView+Frame.h"
#import "CFMacro.h"
#import "UserInfoViewController.h"
#import "SetPassViewController.h"

@interface CheckCodeViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *promptLabel;
@property (weak, nonatomic) IBOutlet UITextField *checkCodeTextField;
@property (weak, nonatomic) IBOutlet UIButton *timeButton;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation CheckCodeViewController

#pragma mark - 重新发送验证码
- (IBAction)resendCheckCode:(id)sender {
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    if (button.tag == 10086) {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
            return parameters;
        }];
        NSDictionary *sendCode = @{@"contact":_contact,
                                   @"reason":@"1",};
        
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:@"38" forHTTPHeaderField:@"Content-Length"];
        [manager.requestSerializer setValue:@"api.kufm.cn" forHTTPHeaderField:@"Host"];
        [manager.requestSerializer setValue:@"ios/4.3.0.0.0/appstore/release" forHTTPHeaderField:@"X-API-ID"];
        [manager.requestSerializer setValue:@"d0ce47483e6728610311619cca3fb195" forHTTPHeaderField:@"X-APP-ID"];
        
        [manager POST:@"http://api.kufm.cn/v2/user/send_check_code"
           parameters:sendCode
             progress:^(NSProgress * _Nonnull uploadProgress) {
                 
             } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                 
             } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             }];
        NSLog(@"重新发送验证码");
    } else {
        NSLog(@"不能发送验证码按钮");
    }
}
#pragma mark - 验证码是否正确
- (IBAction)vertifyCheckCode:(id)sender {
    if ([_checkCodeTextField.text isEqualToString:@""]) {
        [self addPromptLabelWithTitle:@"请输入验证码"];
        return;
    }
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    NSDictionary *sendCode = @{@"contact":_contact,
                            @"check_code":_checkCodeTextField.text,
                                @"reason":@"1"};
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"59" forHTTPHeaderField:@"Content-Length"];
    [manager.requestSerializer setValue:@"api.kufm.cn" forHTTPHeaderField:@"Host"];
    [manager.requestSerializer setValue:@"ios/4.3.0.0.0/appstore/release" forHTTPHeaderField:@"X-API-ID"];
    [manager.requestSerializer setValue:@"d0ce47483e6728610311619cca3fb195" forHTTPHeaderField:@"X-APP-ID"];
    
    [manager POST:@"http://api.kufm.cn/v2/user/verify_check_code"
       parameters:sendCode
         progress:^(NSProgress * _Nonnull uploadProgress) {
         } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             NSLog(@"成功: %@", responseObject);
             if (self.isResetPassword) {
                 SetPassViewController *setPassVC = [[SetPassViewController alloc] init];
                 setPassVC.contact = _contact;
                 setPassVC.checkCode = _checkCodeTextField.text;
                 [self.navigationController pushViewController:setPassVC animated:YES];
             } else {
                 UserInfoViewController *userInfoVC = [[UserInfoViewController alloc] init];
                 userInfoVC.contact = _contact;
                 userInfoVC.checkCode = _checkCodeTextField.text;
                 [self.navigationController pushViewController:userInfoVC animated:YES];                 
             }
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             [self addPromptLabelWithTitle:@"验证码错误"];
         }];
}
#pragma mark - 提示条
- (void)addPromptLabelWithTitle:(NSString *)title {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -20, self.view.gx_width, 20)];
    label.text = title;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:11];
    label.gx_centerX = self.view.gx_centerX;
    label.backgroundColor = [UIColor grayColor];
    [self.view addSubview:label];
    // 出现
    [UIView animateWithDuration:0.5 animations:^{
        label.gx_y = 0;
        // 显示2秒
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:2 options:UIViewAnimationOptionCurveLinear animations:^{
            label.gx_y = -20;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
        }];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    NSRange range = NSMakeRange(3, 4);
    NSString *newString = [_contact stringByReplacingCharactersInRange:range withString:@"****"];
    NSString *prompt = [NSString stringWithFormat:@"我们已向号码%@发送了一条验证短信", newString];
    _promptLabel.text = prompt;
    _checkCodeTextField.delegate = self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.location > 4) {
        return NO;
    }
    return YES;
}

- (void)viewDidDisappear:(BOOL)animated {
    NSLog(@"销毁");
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
